/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import comum.Evento;
import comum.IMsgSource;
import comum.InterpretadorMSG;
import comum.MessageDoCliente;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Anderson
 */
public abstract class PainelBase extends JPanel implements Observer {

    protected IMsgSource parent;
    private boolean ehUser = false;
    private boolean ehChannel = false;

    public boolean isEhChannel() {
        return ehChannel;
    }

    public IMsgSource getParentIMsgSorce() {
        return parent;
    }

    public void setEhChannel(boolean ehChannel) {
        if (ehChannel) {
            this.ehUser = false;
        } else {
            this.ehUser = true;
        }

        this.ehChannel = ehChannel;
    }

    public boolean isEhUser() {
        return ehUser;
    }

    public void setEhUser(boolean ehUser) {
        if (ehUser) {
            this.ehChannel = false;
        } else {
            this.ehChannel = true;
        }

        this.ehUser = ehUser;
    }

    public PainelBase() {
        init();
        InterpretadorMSG.getInstance().addObserver(this);
    }

    @Override
    public String toString() {
        return this.getPainelName();
    }
    protected boolean aberto = false;
    protected JTextArea conversa = new JTextArea();
    protected JTextField entrada = new JTextField();

    protected void entradaKeyPressed(java.awt.event.KeyEvent evt) {
        if (evt.getKeyCode() == evt.VK_ENTER) {
            InterpretadorMSG.getInstance().interpretaMsgCliente(new MessageDoCliente(entrada.getText()));
            entrada.setText(null);
        }
    }

    public void init() {
        conversa.setEditable(false);
        entrada.addKeyListener(new java.awt.event.KeyAdapter() {

            public void keyPressed(java.awt.event.KeyEvent evt) {
                entradaKeyPressed(evt);
            }
        });
    }

    public void setAberto(boolean v) {
        aberto = v;
    }

    protected void abrir() {
    }

    protected void fechar() {
    }

    protected void pisca() {
    }

    public boolean isAberto() {
        return aberto;
    }

    protected void addLinhaTexto(String texto) {
        if (!texto.endsWith("\n")) {
            texto = texto + "\n";
        }
        this.conversa.append(texto);
    }

    protected abstract void atualiza(Evento evento);

    protected abstract String getPainelName();

    protected abstract PainelBase getInstanciaPainel();

    public void update(Observable o, Object arg) {
        if (arg instanceof Evento) {
            atualiza(((Evento) arg));
        }
    }
}
